package org.boulderfoodrescue.webapp.client.multipleaddresspanel;

import com.google.gwt.core.client.JsArray;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.maps.client.geocode.Placemark;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Label;

public class MultipleAdressDialog extends DialogBox{
  protected AddressCellTable table;
  protected Placemark selected = null;
  protected MultipleAdressDialog multipleAdressDialog;
  public MultipleAdressDialog(JsArray<Placemark> locations){
    multipleAdressDialog = this;
    FlowPanel panel = new FlowPanel();
    table = new AddressCellTable(this);
    for(int i=0;i<locations.length();++i){
      table.addPlacemark(locations.get(i));
    }
    Label instructions = new Label("Multiple addresses returned for your search. Choose the correct one.");
    panel.add(instructions);
    panel.add(table);
    Button close = new Button("Cancel");
    Button select = new Button("Select");
    panel.add(close);
    panel.add(select);
    close.addClickHandler(new ClickHandler(){
      @Override
      public void onClick(ClickEvent event) {
        onClose();
      }
    });
    select.addClickHandler(new ClickHandler(){
      @Override
      public void onClick(ClickEvent event) {
        if(selected != null){
          onClose();
        } else{
          Window.alert("No address selected");
        }
      }  
    });
    this.add(panel);
  }
  public void onLocationSelection(Placemark selected){
    this.selected = selected;
  }
  public void onClose(){
    this.hide();
  }
  public Placemark getSelected()
  {
	  return selected;
  }
}
